from flask import Blueprint, jsonify
from ..database import db
from datetime import datetime

bp = Blueprint('show', __name__, url_prefix='/show')


# a simple page that says hello
@bp.route('/cards', methods=['GET'])
def show():
    db.reflect()
    # 1、映射app数据库中的表（app其实就是本程序的flask实例，已连接到数据库）

    tables = db.metadata.tables
    # 2、取得所有数据库（返回：immutabledict，里面实际包含了数据库中所有表的结构

    print(tables)
    cards_info = [x._asdict() for x in db.session.query(tables["cards"]).all()]
    cards = []
    for info in cards_info:
        name = info['name']
        content = [x._asdict() for x in db.session.query(tables[name]).order_by(db.desc("time")).all()]
        cards.append({
            'name': info['cardname'], 'url': info['url'], 'updatetime': info['updatetime'], 'content': content})
    # cards.append(cards_info)
    return jsonify({'cards': cards})


